#include "Well.h"
#include "Configuration.h"
#include "Row.h"
#include "Piece.h"
#include "SoundWrapper.h"

Well::Well(unsigned int widthOfWell)
{
    Size size = Size(widthOfWell, 20).scale(Configuration::BLOCK_SIZE);
    setTexture(GraphicsWrapper::fillRectCached(size, Color(0, 0, 0, 150)));
    setPosition(Position(370 - ((widthOfWell * Configuration::BLOCK_SIZE) / 2), 0));
}

Well::~Well()
{
}

void Well::handleEvent(const Event& event)
{
    event.visitWell(this);
}

void Well::handleAddPieceEvent(long pieceId, const Position& position, const Color& color)
{
    addChild(new Piece(pieceId, position, color));
}

void Well::handleAddRowEvent(long rowId, int rowNo)
{
    addChild(new Row(rowId, rowNo));
}

void Well::handleRemoveRowEvent(long rowId)
{
    Row* row = (Row*)findChild(rowId);
    SoundWrapper::playSound("bang");
    row->runFadeOutAnimation(rowId);
}

void Well::handleRemovePieceEvent(long pieceId)
{
    removeChild(pieceId, true);
}

void Well::handleAnimationDoneEvent(long rowId)
{
    if (findChild(rowId) != NULL)
    {
        removeChild(rowId, true);
    }
}
